package com.hz.filemanager.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hz.filemanager.R;
import com.hz.filemanager.model.navigationItem.NavigationItem;

/**
 * 主页侧滑栏条目适配器
 */
public class MainNavigationAdapter extends BaseAdapter {

    private NavigationItem[] navigationItems = new NavigationItem[]{new NavigationItem(R.mipmap.icon_terms, R.string.terms_of_service),
            new NavigationItem(R.mipmap.icon_privacy, R.string.privacy_policy),
            new NavigationItem(R.mipmap.icon_rate, R.string.rate_us_5_stars)};

    @Override
    public int getCount() {
        return navigationItems.length;
    }

    @Override
    public Object getItem(int position) {
        return navigationItems[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NavigationItemViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new NavigationItemViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_item,parent,false);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (NavigationItemViewHolder) convertView.getTag();
        }
        viewHolder.im.setImageResource(navigationItems[position].getIcon());
        viewHolder.tv.setText(parent.getContext().getString(navigationItems[position].getName()));
        return convertView;
    }

    @Override
    public boolean isEnabled(int position) {
        if (position == -1) {
            return false;
        }
        return true;
    }

    private static final class NavigationItemViewHolder {
        ImageView im;
        TextView tv;
    }
}
